# ** por compatibilidad se omiten tildes **
# ==============================================================================
# TRABAJO PRACTICO 3 - System Programming - ORGANIZACION DE COMPUTADOR II - FCEN
# ==============================================================================

MCOPY=mcopy

KERNEL_SRC=kernel.asm
KERNEL_OBJ=kernel.o
KERNEL_BIN=kernel.bin

DEPEND=kernel.asm macrosmodoreal.mac macrosmodoprotegido.mac gdt.c pic.c idt.c isr.asm tss.c mmu.c Makefile gdt.h pic.h idt.h isr.h tss.h mmu.h sched.h
OBJ= $(KERNEL_OBJ)  gdt.o pic.o idt.o isr.o mmu.o sched.o tss.o screen.o rand.o
DISK_IMG=diskette.img

CFLAGS=-m32 -g -ggdb -Wall -Werror -O0 \
  -fno-zero-initialized-in-bss -fno-stack-protector -ffreestanding

#REMOVE
TASK0=idle.tsk
TASK1=task1.ctsk
TASK2=task2.ctsk
TASK3=task3.ctsk
TASK4=task4.ctsk
TASK5=task5.ctsk
TASKSBINS=$(TASK0) $(TASK1) $(TASK2) $(TASK3) $(TASK4) $(TASK5)
SCREENBUFFER=screen.buf
#REMOVE END
TASKS=tareas.tsk
KERNELSIZE=85504
CODESIZE=60928
TASKSSIZE=24576

OBJDUMP=objdump
OBJCOPY=objcopy
CC=gcc
NASM=nasm
NASMFLAGS=-felf32
LD=ld               
LDTASKFLAGS=-s -static -m elf_i386 --oformat binary -b elf32-i386 -e start -Ttext 0x3A0000
LDFLAGS=-static -m elf_i386 --oformat binary -b elf32-i386 -e start -Ttext 0x1200

.PHONY=clean all image tasks

all: cleantasks tasks image 
# clean

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $^

%.o: %.asm
	$(NASM) $(NASMFLAGS) -o $@ $^

kernel.bin: $(OBJ) $(TASKS)
	@echo 'Linkeando el kernel...'
	$(LD) $(LDFLAGS) -o $@ $(OBJ)
	@echo ''
	mv kernel.bin kernel.bin.tmp
	dd if=/dev/zero bs=1 count=$(KERNELSIZE) of=kernel.bin status=noxfer
	dd if=kernel.bin.tmp bs=1 count=$(CODESIZE) of=kernel.bin conv=notrunc status=noxfer
	dd if=$(TASKS) bs=1 count=$(TASKSSIZE) seek=$(CODESIZE) conv=notrunc of=kernel.bin status=noxfer

image: kernel.bin $(DEPEND)
	@echo 'Copiando el $(KERNEL_BIN) a la imagen de diskette\n'
	$(MCOPY) -o -i $(DISK_IMG) $(KERNEL_BIN) ::/ 

clean:
	rm -f *.bin *.o *.ctsko *.ctsk *.tsko kernel.bin.tmp

#REMOVE

task1.tsko: task.c
	$(CC) $(CFLAGS) -Dtask1 -c -o $@ $^ 
task2.tsko: task.c
	$(CC) $(CFLAGS) -Dtask2 -c -o $@ $^ 
task3.tsko: task.c
	$(CC) $(CFLAGS) -Dtask3 -c -o $@ $^ 
task4.tsko: task.c
	$(CC) $(CFLAGS) -Dtask4 -c -o $@ $^ 
task5.tsko: task.c
	$(CC) $(CFLAGS) -Dtask5 -c -o $@ $^ 

%.ctsk: %.tsko
	dd if=/dev/zero bs=1 count=4096 of=$@
	$(LD) $(LDTASKFLAGS) -o $@.tmp $^
	dd if=$@.tmp bs=1 count=4096 seek=0 conv=notrunc of=$@
	rm $@.tmp

%.tsk: %.asm
	$(NASM) -fbin -o $@ $^

tasks: $(TASKSBINS)
	dd if=/dev/zero bs=1 count=$(TASKSSIZE) of=$(TASKS) status=noxfer
	dd if=$(TASK0) bs=1 count=4K of=$(TASKS) seek=0 status=noxfer
	dd if=$(TASK1) bs=1 count=4K of=$(TASKS) seek=4K status=noxfer
	dd if=$(TASK2) bs=1 count=4K of=$(TASKS) seek=8K status=noxfer
	dd if=$(TASK3) bs=1 count=4K of=$(TASKS) seek=12K status=noxfer
	dd if=$(TASK4) bs=1 count=4K of=$(TASKS) seek=16K status=noxfer
	dd if=$(TASK5) bs=1 count=4K of=$(TASKS) seek=20K status=noxfer

cleantasks:
	rm -f *.tsk
	rm -f *.ctsk
	rm -f *.tsko

#REMOVE END
